/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.tpj.database.obrazky;

import cz.tpj.database.DatabaseConnector;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import cz.tpj.util.ErrorDatabase;



/**
 * Trieda sprostredkuvava metody pre vyberanie Obrazkov z databazy
 * @author jabo
 */
public class Table_picture {
    //------------------finalni promenne----------------------------------------
    public static String TABLE = "obrazky";
    public static String ID    = "id";
    public static String PATH  = "cesta";
    //
    private static Table_picture im = null;
    private Connection con;
    private Statement stmt = null;
    //-------------------------------------------------------------------------
    //======================konstruktor================
    private Table_picture(){}

    /**
     * Vrací jedinou instanci této třídy.
     * @return Table_city
     */
    
    public static Table_picture getInstance(){
        if(im == null){
            im = new Table_picture();
        }
        return im;
    }
    //=======================metody-------------------------
    private void databaseConnect() throws SQLException{
        DatabaseConnector dc = DatabaseConnector.getInstance();
        con = dc.getConnection();
        stmt = con.createStatement();
    }
    //--------------------------------------------------------------------------
    
    /**
     * vrati obrazok z databazy, ktory ma rovnake id ako je id zadane parametrom
     * @return  objekt Obrazok
     * @throws ErrorDatabase
     */
    
    public Obrazok getPictureFromId(String pictureId) throws ErrorDatabase {
        ResultSet rs = null;
        Obrazok obr = new Obrazok();
        try {
            databaseConnect();
            rs = stmt.executeQuery("SELECT * FROM "+TABLE+" " +
                                   "WHERE "+ID+" = "+pictureId+";");

            if(rs.next()){
                obr.setId(rs.getString(ID));
                obr.setCesta(rs.getString(PATH));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Table_picture.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            try {
                rs.close();
                stmt.close();
            } catch (Exception ex) {
            }
        }
        return obr;
    }
    
    /**
     * vracia posledny obrazok v databaze
     * @return
     * @throws ErrorDatabase
     */
    public String getLastImage() throws ErrorDatabase{
    	String ret = "0";
        ResultSet rs = null;    	
        try {
            databaseConnect();
            rs = stmt.executeQuery("SELECT * FROM "+TABLE+" ORDER BY id DESC LIMIT 0,1");

            if(rs.next()){
                ret = rs.getString(ID);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Table_picture.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            try {
                rs.close();
                stmt.close();
            } catch (Exception ex) {
            }
        }
        return ret;
    	
    }

    /**
     * vlozi obrazok do databazy
     * @return
     * @throws ErrorDatabase
     */
	public String insertPicture(Obrazok vkladane) throws ErrorDatabase{			
		String imageID = null;
		boolean ok = false;
		
        try {
            databaseConnect();
            String s = "INSERT INTO " + TABLE + "" +
                    "(" + PATH + ")" +
                    "VALUES ('" + vkladane.getCesta() + "')";

            int updated = stmt.executeUpdate(s);
            //pokud
            //1) byl zmenen jeden radek, pak potvrzeni transakce
            //2) bylo zmeneno vice a nebo zadny, pak vratit transakci zpet
            if(updated == 1){
                con.commit(); //potvrzeni transakce
                ok = true;
            }else{
                con.rollback(); //vraceni transakce
            }
        } catch (SQLException ex) {
            String s = ex.getMessage();
            throw new ErrorDatabase(ErrorDatabase.ERROR_DATABASE_CONECTION, "Cbyba pri praci s databazovym serverem." + s);
        } finally{
            try {
                stmt.close();
            } catch (SQLException ex) {}
        }
        
        //vytiahni image ID
        if (ok == true) {
        	imageID = getLastImage();
        }
        
        
		return imageID;
	}
}
